package catalina.container;

import catalina.startup.LifeCycle;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public abstract class ContainerBase implements Container,LifeCycle {
    private String name;

    private Container parent;

    private List<Container> containerList= new ArrayList();

    private Properties properties;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setParent(Container container) {
        this.parent = container;
    }

    @Override
    public Container getParent() {
        return this.parent ;
    }

    @Override
    public void addChild(Container child) {
        containerList.add(child);
    }

    @Override
    public Container findChild(String name) {
        for (Container container : containerList) {
            if(container.getName().equals(name)){
                return container;
            }
        }
        return null;
    }

    @Override
    public List<Container> findChildren() {
        return containerList;
    }

    @Override
    public Properties getProperties() {
        return properties;
    }

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public void init() {
        initInternal();
        for (Container container : containerList) {
            container.init();
        }
    }

    @Override
    public void start() {
        startInternal();
        for (Container container : containerList) {
            container.start();
        }
    }

    @Override
    public void stop() {
        stopInternal();
        for (Container container : containerList) {
            container.stop();
        }
    }

    @Override
    public void destroy() {
        destroyInternal();
        for (Container container : containerList) {
            container.destroy();
        }
    }

    public abstract void initInternal();
    public abstract void startInternal();
    public abstract void stopInternal();
    public abstract void destroyInternal();
}
